<?php

require_once 'WoolyService.php';
require_once 'PHPUnit/Framework.php';

class ServiceWoolyTest extends PHPUnit_Framework_TestCase {
  
  const PARAM_APIKEY  = 'test1234test1234';
  const PARAM_LONGURL = 'http://www.test1234test1234.com/test1234test1234.html';
  const PARAM_ID      = 1234;
  const PARAM_MINI    = 'test1234';
  const PARAM_CUSTOM  = 'test1234';
  
  /**
   * @var WoolyService
   */
  protected $service;
  
  public function setUp() {
    $this->service = new WoolyService(self::PARAM_APIKEY);
  }
  
  /**
   * @dataProvider providerActions
   */
  public function testRequestActionExists($action) {
    $this->assertTrue(method_exists($this->service, $action));
  }
  
  /**
   * @dataProvider providerRequests
   */
  public function testRequestAccepted($request) {
    $action = array_shift($request);
    $this->assertTrue(call_user_func_array(array($this->service, $action), $request));
  }
  
  public function providerActions() {
    return $this->service->getValidActions();
  }
  
  public function providerRequests() {
    $actions = $this->providerActions();
    $requests = array();
    foreach($actions as $action) {
      $gen_requests = call_user_func(array($this, 'providerRequestParams'.ucwords($action)));
      array_unshift($gen_requests, $action);
      $requests = array_merge(
        $requests, $gen_requests
      );
    }
    return $requests;
  }
  
  public function providerRequestParamsCreate() {
    $request = array();
    $values = array(null, false, true);
    $optcnt = count($values);
    $combos = 2 * pow($optcnt, 2);
    for($i=0; $i<18; $i++) {
      $request[] = array(
        self::PARAM_LONGURL, ($i<($combos/2)?null:self::PARAM_CUSTOM), $values[floor($i/3)%3], $values[$i%3]
      );
    }
    return $request;
  }
  
  public function providerRequestParamsGet() {
    return array(
      array(self::PARAM_ID),
      array(self::PARAM_MINI)
    );
  }
  
  public function providerRequestParamsDelete() {
    return array(
      array(self::PARAM_ID),
      array(self::PARAM_MINI)
    );
  }
  
  public function providerRequestParamsStats() {
    return array(
      array(null)
    );
  }
  
  public function providerRequestParamsUsage() {
    return array(
      array(null),
      array(self::PARAM_APIKEY)
    );
  }
  
  public function providerRequestParamsHelp() {
    $request = array(array(null));
    $methods = $this->providerMethodNames();
    foreach($methods as $method) {
      $request[] = array($method);
    }
    return $request;
  }
  
  public function providerRequestParamsDump() {
    
    $start_time = time()-86400;
    $end_time = time();
    $start_date = date(DATE_W3C, $start_time);
    $end_date = date(DATE_W3C, $start_time);
    
    $request = array(
      array(null),
      array($start_time, $end_time),
      array($start_date, $end_date),
      array($start_time, $end_date),
      array($start_date, $end_time),
    );
    
    $ranges = $this->service->getValidRanges();
    foreach($ranges as $range) {
      $request[] = array($range);
    }
    return $request;
  }
}
